<template>
  <div>
    <div class="toolsbox">
      <div id="bar" style="width:700px;height:300px;margin:0 auto;position:relative;z-index: 1" class="bar"></div>
    </div>
    <div class="insert">
<!--      <span class="refeachat" @click="echatsRefesh">重绘</span>-->
      <span class="use" @click="insert">使用</span>
    </div>
  </div>
</template>

<script>

import * as echarts from 'echarts'

export default {
  name: 'Liuyan',
  data() {
    return {
      chart: '',
      imgUrl: ''
    }
  },
  props: {
    title: {
      type: String
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    dataCount: {
      type: Object
    }
  },
  watch: {
    'dataCount':{
      handler:function(n, o) {
        this.init()
      },
      deep: true
    }
  },

  methods: {
    insert() {
      this.$emit('backdata', this.imgUrl)
    },
    async init() {
      this.chart = echarts.init(document.getElementById('bar'))
      const ChartTestData = {
        title: {
          text: this.title,
          left: 'center',
          subtextStyle: {
            color: '#666'
          },
          textStyle: {
            color: '#666'
          }
        },
        grid:{
          // bottom:'1',

        },
        graphic: [
          {
            type: 'text',
            left: 'center',
            bottom: '0',
            style: {
              text: '收件量=当月新增群众诉求数量；办结量=在当月办结的诉求数量；按时办结量=在留言办理时效内按期办结的诉求数量',
              fill: '#666',
              fontSize: 12
            }
          }
        ],
        animation: false,
        xAxis: {
          type: 'category',
          data: ['收件量', '办结量', '按时办结量']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [this.dataCount.shoudao, this.dataCount.banjie, this.dataCount.anshibanjie],
            type: 'bar',
            barWidth: '40',
            label: {
              show: true,
              position: 'top',
              textStyle: {
                color: '#000',
                fontSize: 12
              }
            }
          }
        ]
      }
      await this.chart.setOption(ChartTestData)
      var baseImage = this.chart.getDataURL('png')
      this.imgUrl = baseImage
      window.addEventListener('resize', this.chart.resize)
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style scoped lang="less">
.toolsbox {
  position: relative;
  padding: 10px 0;
  margin: 0 20px;
}

.insert {
  margin-top: 10px;
  font-size: 14px;
  z-index: 999;
  width: 100%;
  display: flex;
  justify-content: center;

  .use {
    background-color: #ecf5ff;
    border: 1px solid #d9ecff;
    padding: 10px 30px;
    font-size: 14px;
    color: #409EFF;
    border-radius: 10px;
    cursor: pointer;

    &:hover {
      background-color: #d9ecff;
    }
  }
  .refeachat {
    margin-right:20px;
    font-size: 14px;
    z-index: 999;
    margin-left: 50px;
    border: 1px solid #d9ecff;
    padding: 10px 30px;
    color: #409EFF;
    border-radius: 10px;
    cursor: pointer;
  }
}

</style>
